package com.by.component.xfilestorage;

import com.by.component.tencent.TencentAccessProperties;
import com.by.component.tencent.cos.TencentCosProperties;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileStorageService;
import org.dromara.x.file.storage.core.platform.FileStorage;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

import javax.annotation.Resource;
import java.util.concurrent.CopyOnWriteArrayList;

@Slf4j
@Configuration
public class XFileStorageConfig {

    @Resource
    private FileStorageService fileStorageService;

    @Resource
    private TencentAccessProperties tencentAccessProperties;

    @Resource
    private TencentCosProperties tencentCosProperties;

    @EventListener(ApplicationReadyEvent.class)
    public void initStorages(ApplicationReadyEvent event) {
        log.info("init xFileStorage start");
        CopyOnWriteArrayList<FileStorage> storages = fileStorageService.getFileStorageList();
        log.info("[XFileStorageConfig][initStorages] addBefore storages:[{}]", storages);
        /*List<ResourcesTypeBasicSDTO> resourcesTypeBasics = staticResourcesMapper.resourcesTypeBasic(null, null);

        Arrays.stream(StaticResourcesPlatformTypeEnum.values())
                .forEach(en -> {
                    Map<String, List<String>> bucketDomainMap = resourcesTypeBasics.stream()
                            .filter(i -> en.getCode().equals(i.getPlatformType()))
                            .collect(Collectors.groupingBy(ResourcesTypeBasicSDTO::getBucketName, Collectors.mapping(ResourcesTypeBasicSDTO::getDomainName, Collectors.toList())));

                    if (StaticResourcesPlatformTypeEnum.ALIYUN.getCode().equals(en.getCode())) {
                        storages.addAll(FileStorageServiceBuilder.buildAliyunOssFileStorage(bucketDomainMap.entrySet()
                                .stream()
                                .map(e -> {
                                    String bucketName = e.getKey();
                                    String domainName = e.getValue().get(0);
                                    FileStorageProperties.AliyunOssConfig config = new FileStorageProperties.AliyunOssConfig();
                                    config.setPlatform(bucketName);
                                    config.setAccessKey(aliyunAccessProperties.getAccessKey());
                                    config.setSecretKey(aliyunAccessProperties.getSecretKey());
                                    config.setEndPoint(aliyunOssProperties.getEndpoint());
                                    config.setBucketName(bucketName);
                                    config.setDomain(domainName);
                                    config.setBasePath("/");
                                    return config;
                                }).collect(Collectors.toList()), null));
                    }

                    if (StaticResourcesPlatformTypeEnum.TENCENT.getCode().equals(en.getCode())) {
                        storages.addAll(FileStorageServiceBuilder.buildTencentCosFileStorage(bucketDomainMap.entrySet()
                                .stream()
                                .map(e -> {
                                    String bucketName = e.getKey();
                                    String domainName = e.getValue().get(0);
                                    FileStorageProperties.TencentVodConfig config = new FileStorageProperties.TencentVodConfig();
                                    config.setPlatform(bucketName);
                                    config.setSecretId(tencentAccessProperties.getSecretId());
                                    config.setSecretKey(tencentAccessProperties.getSecretKey());
                                    config.setRegion(tencentCosProperties.getRegion());
                                    config.setBucketName(bucketName);
                                    config.setDomain(domainName);
                                    config.setBasePath("/");
                                    return config;
                                }).collect(Collectors.toList()), null));
                    }
                });*/
        log.info("[XFileStorageConfig][initStorages] addBefore storages:[{}]", storages);
        log.info("init xFileStorage end");
    }

}
